local DIY = require "packages/diy_utility/diy_utility"
local ls = require "packages/lvshi/utility"

local guzao = fk.CreateSkill{
  name = "lvshi__guzao",
  tags = {"Phase"}
}

Fk:loadTranslationTable{
  ["lvshi__guzao"] = "鼓噪",
  [":lvshi__guzao"] = "阶段技，你可令一名对方角色将一张牌当【增兵减灶】使用，然后你将其弃置的牌依次当【出其不意】使用。若仅弃置一张，你可改为【万箭齐发】并明置目标角色各一张手牌。",
  ["#lvshi__guzao-prompt"] = "鼓噪：你可以令一名对方角色将一张牌当【增兵减灶】使用",
  ["#lvshi__guzao-use"] = "请将一张牌当【增兵减灶】使用",
  ["#lvshi__guzao-choose"] = "鼓噪：你可改为【万箭齐发】并明置目标角色各一张手牌",
  ["#lvshi__guzao-askuse"] = "鼓噪：请依次当【出其不意】使用，若仅弃置一张可改为【万箭齐发】",
  ["lvshi__guzao_slash"] = "使用【杀】",
  ["lvshi__guzao_snatch"] = "使用【顺手牵羊】",

  ["$lvshi__guzao1"] = "脚踏风雨，手握雷霆，曳断九天银河！",
  ["$lvshi__guzao2"] = "身拄长刀，兀立斜阳，何妨以血补天！",
}

local DIY = require "packages/diy_utility/diy_utility"

guzao:addEffect("active", {
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  max_phase_use_time = 1,
  prompt = "#lvshi__guzao-prompt",
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and player.role_shown and to_select.role_shown and not ls.isFriend(player, to_select) and not to_select:isNude()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:askToUseVirtualCard(target, {
      name = "reinforcement",
      skill_name = guzao.name,
      prompt = "#lvshi__guzao-use",
      cancelable = false,
      card_filter = {
        n = 1,
        cards = target:getCardIds("he"),
      },
    })
    local discards = player:getTableMark("guzao-tmp")
    local name = {"lvshi__unexpectation"}
    if #discards == 1 then
      table.insert(name, "lvs__archery_attack")
    end
    local cards = table.simpleClone(discards)
    room:setPlayerMark(player, "guzao-tmp", 0)
    for i = 1, #cards do
      local usedata = room:askToUseVirtualCard(player, {
        name = name,
        skill_name = guzao.name,
        prompt = "#lvshi__guzao-askuse",
        cancelable = false,
        card_filter = {
          n = 1,
          cards = discards,
        },
        extra_data = { expand_pile = discards },
      })
      if usedata then
        table.removeOne(discards, usedata.card:getEffectiveId(true))
      end
    end
  end,
})

guzao:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player.dead then
      local cards = {}
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard and move.skillName == "reinforcement_skill" then
          local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
          if use_event ~= nil then
            local use = use_event.data
            if use.card.skillName == guzao.name then
              for _, info in ipairs(move.moveInfo) do
                if table.contains(player.room.discard_pile, info.cardId) then
                  table.insertIfNeed(cards, info.cardId)
                end
              end
            end
          end
        end
      end
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(event:getCostData(self).cards) do
      room:addTableMark(player, "guzao-tmp", id)
    end
  end,
})

guzao:addEffect(fk.TargetSpecified, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(guzao.name) and data.card.trueName == "archery_attack"
    and data.card.skillName == guzao.name
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local cards = table.filter(data.to:getCardIds("h"), function (id)
      return not table.contains(DIY.getShownCards(data.to), id)
    end)
    if #cards > 0 then
      DIY.showCards(data.to, table.random(cards, 1))
    end
  end,
})

return guzao